package samples.d2.polygons 
{
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.setInterval;
	import samples.BaseSample;
	import shapes.shapes2D.distribution.Circular;
	import shapes.shapes2D.distribution.Frame;
	import shapes.shapes2D.polygon.ConvexHull;
	import shapes.shapes2D.polygon.Edge;
	import shapes.shapes2D.polygon.Polygon;
	import shapes.shapes2D.polygon.Skeleton;
	
	/**
	 * @author Nicolas Barradeau
	 * http://en.nicoptere.net
	 */
	public class Polygon4 extends BaseSample
	{
		
		private var poly:Polygon;
		public function Polygon4() 
		{
			
			//poly = new Polygon( 0,0, new Circular( new Point( sw/2, sh/2 ), 200, 200 ).distribute( 30, false, 100 ).reverse() );
			poly = new Coq( sw / 2, sh / 2, 3 );
			
			graphics.lineStyle( 10, 0x003399, 1 );
			plotLines( poly.points );
			
			graphics.lineStyle( 0, 0xFFFFFF );
			plotPoints( poly.points );
			
			
			graphics.lineStyle( 2, 0xFFFF00 );
			plotLines( ConvexHull.process( poly.points ) );
			
			
			
			
		}
		
		private function addPoint():void 
		{
			var i:int = 10;
			while ( i-- )
			{
				var p:Point = new Point( Math.random() * sw, Math.random() * sh );
				
				graphics.lineStyle( 4, 0xFF0000 );
				if ( poly.contains( p ) )
				{
					graphics.lineStyle( 4, 0x00FF00 );
				}
				graphics.moveTo( p.x, p.y );
				graphics.lineTo( p.x+.5, p.y );
			}
			
		}
		
		private function plotPoints( points:Vector.<Point>):void
		{
			
			var p:Point = points[ 0 ];
			for each( p in points )
			{
				graphics.drawCircle( p.x, p.y, 2 );
			}
			
		}
		
		private function plotLines( points:Vector.<Point>):void
		{
			if ( points.length == 0 ) return;
			var p:Point = points[ 0 ];
			graphics.moveTo( p.x, p.y );
			
			for each( p in points )
			{
				
				graphics.lineTo( p.x, p.y );
				
			}
			graphics.lineTo( points[ 0 ].x, points[ 0 ].y );
			
		}
		
		private function plotEdges( edges:Vector.<Edge>):void
		{
			if ( edges.length == 0 ) return;
			
			var e:Edge = edges[ 0 ];
			
			for each( e in edges )
			{
				
				graphics.moveTo( e.p0.x, e.p0.y );
				graphics.lineTo( e.p1.x, e.p1.y );
				
			}
			
		}
		private function plotNormals( edges:Vector.<Edge>, left:Boolean = true ):void
		{
			if ( edges.length == 0 ) return;
			
			var e:Edge;
			var n:Edge;
			
			for each( e in edges )
			{
				if ( left ) n = e.leftNormal;
				else n = e.rightNormal;
				
				graphics.moveTo( n.p0.x, n.p0.y );
				graphics.lineTo( n.p1.x, n.p1.y );
				
			}
			
		}
		
	}

}